package com.voodoo.lib_http.builder;

import com.voodoo.lib_http.request.PostFromRequest;
import com.voodoo.lib_http.request.RequestCall;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: voodoo
 * CreateDate: 2021/05/29
 * Description:
 */
public class PostFormBuilder extends OkHttpRequestBuilder<PostFormBuilder> implements HasParamsable {

    List<FileInput> files = new ArrayList<>();

    @Override
    public RequestCall build() {
        return new PostFromRequest(url, tag, params, headers, files, id).build();
    }

    public PostFormBuilder files(String key, Map<String, File> files) {
        for (String fileName : files.keySet()) {
            this.files.add(new FileInput(key, fileName, files.get(fileName)));
        }
        return this;
    }

    public PostFormBuilder addFile(String key, String fileName, File file) {
        files.add(new FileInput(key, fileName, file));
        return this;
    }

    public static class FileInput {
        public String key;
        public String fileName;
        public File file;

        public FileInput(String key, String fileName, File file) {
            this.key = key;
            this.fileName = fileName;
            this.file = file;
        }

        @Override
        public String toString() {
            return "FileInput{" +
                    "key='" + key + '\'' +
                    ", fileName='" + fileName + '\'' +
                    ", file=" + file +
                    '}';
        }
    }

    @Override
    public OkHttpRequestBuilder params(Map<String, Object> params) {
        this.params = params;
        return this;
    }

    @Override
    public OkHttpRequestBuilder addParams(String key, Object value) {
        if (this.params == null) {
            params = new LinkedHashMap<>();
        }
        params.put(key, value);
        return this;
    }

}
